const md5 = require('md5')
const executeSql = require('../utils/query.js')
const { PASSWORD_SALT } = require('../config/index.js')

exports.updAvatar = async (admin_id, avatar) => {
  // 1. 查数据库
  let sql = `update manager set avatar = '${avatar}' where admin_id = ${admin_id}`
  let result = await executeSql(sql)
  return result
}

exports.getUserInfo = async (admin_id) => {
  // 1. 查数据库
  let sql = `select admin_id,username,avatar,nickname,add_date,last_login_date from manager where admin_id = ${admin_id}`
  let result = await executeSql(sql)
  return result
}

exports.Login = async (username, password) => {
  password = md5(password + PASSWORD_SALT)
  // 操作数据库 ,编写sql,真实业务中，肯定要去数据库中匹配
  let sql = `select * from manager where username='${username}' and password = '${password}' limit 1`
  let result = await executeSql(sql)
  return result
}

exports.getAdminUserInfoById = async (id) => {
  // 操作数据库 ,编写sql,真实业务中，肯定要去数据库中匹配
  let sql = `select * from manager where admin_id = ${id} limit 1`
  let result = await executeSql(sql)
  return result
}

exports.getAdminList = async () => {
  // 操作数据库 ,编写sql,真实业务中，肯定要去数据库中匹配
  let sql = `select admin_id,username,nickname from manager`
  let result = await executeSql(sql)
  return result
}
